package org.jypj.dev.util;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class EchartToos {

	/**
	 * 获取柱状图
	 * @param paramObject
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String getEchartBarJson(Map<String, Object> paramObject) {
		JSONObject outData = new JSONObject();

		// 定义标题
		JSONObject node = new JSONObject();
		if (paramObject.get("title") != null) {
			node.put("text", paramObject.get("title"));
			outData.put("title", node);
		}
		// 提示框
		node = new JSONObject();
		node.put("trigger", "axis");
		outData.put("tooltip", node);

		// 图例
		node = new JSONObject();
		node.put("data", paramObject.get("legend"));
		outData.put("legend", node);

		// calculable设置
		Boolean calculable = true;
		outData.put("calculable", calculable);

		// xAxis 轴设置
		JSONArray jsonArray = new JSONArray();
		node = new JSONObject();
		node.put("type", "category");
		node.put("data", paramObject.get("xAxis"));
		jsonArray.add(node);
		outData.put("xAxis", jsonArray);

		// yAxis 设置
		jsonArray = new JSONArray();
		node = new JSONObject();
		node.put("type", "value");
		jsonArray.add(node);
		outData.put("yAxis", jsonArray);
		// series   //设置数据
		Map<String, Integer[]> map = (LinkedHashMap) paramObject.get("dataValue");
		jsonArray = new JSONArray();
		for (Map.Entry<String, Integer[]> entry : map.entrySet()) {
			node = new JSONObject();
			node.put("name", entry.getKey());
			node.put("type","bar" );
			node.put("data",entry.getValue());
			jsonArray.add(node);
			outData.put("series", jsonArray);
		}
		String toolbox="";
		//设置工具类
		if(paramObject.get("tooldisable")==null)
		{
			 toolbox="\"toolbox\": {\"show\" : true,\"feature\" : {\"mark\" : {\"show\": true},\"dataView\" : {\"show\": true, \"readOnly\": false},\"magicType\" : {\"show\": true, \"type\": [\"line\",\"bar\"]},\"restore\" : {\"show\": true},\"saveAsImage\" : {\"show\": true}}}";
			
			
		}
		String varStr=outData.toString();
		if(paramObject.get("tooldisable")==null)
		{
			varStr=varStr.substring(1,varStr.length());
			varStr=varStr.substring(0,varStr.length()-1);
			return "{"+varStr+","+toolbox+"}";
			
			
		}else
		{
			return varStr;
		}
	
		
	}
	
	
	/**
	 * 获取饼状图
	 * @param paramObject
	 * @return
	 */
	 @SuppressWarnings({ "unchecked"})
	 public static String getPiechart(Map<String, Object> paramObject)
	 {
			JSONObject outData = new JSONObject();
			// 定义标题
			JSONObject node = new JSONObject();
			if (paramObject.get("title") != null) {
				node.put("text", paramObject.get("title"));
				node.put("x", "center");
				outData.put("title", node);
			}
			// 提示框
			String tip="\"tooltip\" : {\"trigger\": \"item\",\"formatter\": \"{a} <br/>{b} : {c} ({d}%)\"}";

			// 图例
			node = new JSONObject();
			node.put("data", paramObject.get("legend"));
			node.put("orient", "vertical");
			node.put("x", "left");
			outData.put("legend", node);
			// calculable设置
			Boolean calculable = true;
			outData.put("calculable", calculable);
			// series   //设置数据
			node = new JSONObject();
			Map<String, Integer> map = (LinkedHashMap<String,Integer>) paramObject.get("dataValue");
			JSONArray jsonArray = new JSONArray();
			JSONArray sArray = new JSONArray();
			for (Map.Entry<String, Integer> entry : map.entrySet()) {
				JSONObject snode = new JSONObject();
				snode.put("value", entry.getValue());
				snode.put("name", entry.getKey());
				sArray.add(snode);
			}
			node.put("name", paramObject.get("smaltitle"));
			node.put("type","pie" );
			node.put("radius","55%" );
			node.put("center", new String[]{"50%","60%"});
			node.put("data", sArray);
			jsonArray.add(node);
			outData.put("series", jsonArray);
			//设置工具类
			String toolbox="\"toolbox\": {\"show\" : true,\"feature\" : { \"mark\" : {\"show\": true},\"restore\" : {\"show\": true},\"saveAsImage\" : {\"show\": true}}}";
			//String toolbox="\"toolbox\": {\"show\" : true,\"feature\" : {\"mark\" : {\"show\": true},\"dataView\": {\"show\": true, \"readOnly\": false},\"magicType\" : {\"show\": true, \"type\": [\"pie\", \"funnel\"],\"option\": {\"funnel\": {\"x\": \"25%\",\"width\": \"50%\",\"funnelAlign\": \"left\",\"max\": 1548}}},\"restore\" : {\"show\": true},\"saveAsImage\" : {\"show\": true} }}";
			String varStr=outData.toString();
			varStr=varStr.substring(1,varStr.length());
			varStr=varStr.substring(0,varStr.length()-1);
			return "{"+varStr+","+toolbox+","+tip+"}";
		
	 }
	 
	 /**
	  * 获取柱状堆砌图
	  * @param paramObject
	  * @return
	  */
     @SuppressWarnings({ "unchecked", "rawtypes" })
	public static String getBarStack(Map<String, Object> paramObject)
	 {

 		JSONObject outData = new JSONObject();

 		// 定义标题
 		JSONObject node = new JSONObject();
 		if (paramObject.get("title") != null) {
 			node.put("text", paramObject.get("title"));
 			outData.put("title", node);
 		}
 		// 提示框
 	    String tip="\"tooltip\" : {\"trigger\":\"axis\",\"axisPointer\" :{\"type\" :\"shadow\"}}";

 		// 图例
 		node = new JSONObject();
 		node.put("data", paramObject.get("legend"));
 		outData.put("legend", node);

 		// calculable设置
 		Boolean calculable = true;
 		outData.put("calculable", calculable);

 		// xAxis 轴设置
 		JSONArray jsonArray = new JSONArray();
 		node = new JSONObject();
 		node.put("type", "category");
 		node.put("data", paramObject.get("xAxis"));
 		jsonArray.add(node);
 		outData.put("xAxis", jsonArray);

 		// yAxis 设置
 		jsonArray = new JSONArray();
 		node = new JSONObject();
 		node.put("type", "value");
 		jsonArray.add(node);
 		outData.put("yAxis", jsonArray);
 		// series   //设置数据
		Map<String, Integer[]> map = (LinkedHashMap) paramObject.get("dataValue");
 		jsonArray = new JSONArray();
 		for (Map.Entry<String, Integer[]> entry : map.entrySet()) {
 			node = new JSONObject();
 			node.put("name", entry.getKey());
 			node.put("type","bar" );
 			node.put("stack","stack" );
 			node.put("data",entry.getValue());
 			jsonArray.add(node);
 			outData.put("series", jsonArray);
 		}
 		//设置工具类
 		String toolbox="\"toolbox\": {\"show\" : true,\"orient\": \"vertical\",\"x\": \"right\",\"y\": \"center\",\"feature\" : {\"mark\" : {\"show\": true},\"dataView\" : {\"show\": true, \"readOnly\": false},\"magicType\" : {\"show\": true, \"type\": [\"line\", \"bar\", \"stack\", \"tiled\"]},\"restore\" : {\"show\": true},\"saveAsImage\" : {\"show\": true}}}";
 		
 		String varStr=outData.toString();
 		varStr=varStr.substring(1,varStr.length());
 		varStr=varStr.substring(0,varStr.length()-1);
 		return "{"+varStr+","+toolbox+","+tip+"}";
 	
	 }
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Integer[]> dataValue = new LinkedHashMap<String, Integer[]>();
		Integer[] value1 = new Integer[] { 20, 30, 40, 50 };
		dataValue.put("女", value1);
		map.put("title", "考勤数量");// 标题
		map.put("xAxis", new String[] { "一年级", "二年级", "三年级", "四年级" }); // 横轴
		map.put("dataValue", dataValue);
		System.out.println(EchartToos.getEchartBarJson(map));
	}
	
	
	
}
